package com.itcam.cammybibackend.manager;

import com.itcam.cammybibackend.common.ErrorCode;
import com.itcam.cammybibackend.constant.PromptConstant;
import com.itcam.cammybibackend.exception.BusinessException;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.SSEResponseModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.lkeap.v20240522.LkeapClient;
import com.tencentcloudapi.lkeap.v20240522.models.ChatCompletionsRequest;
import com.tencentcloudapi.lkeap.v20240522.models.ChatCompletionsResponse;
import com.tencentcloudapi.lkeap.v20240522.models.Message;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : Cammy.Wu
 * Description : todo:用于对接AI平台
 */

@Service
@Slf4j
public class AiManager {

    @Resource
    private LkeapClient deepSeekClient;

    /**
     * AI 对话
     *
     * @param message
     * @return
     */
    public String doChat(String message) {
        try {
            // 实例化一个请求对象,每个接口都会对应一个request对象
            ChatCompletionsRequest req = new ChatCompletionsRequest();
            req.setModel("deepseek-v3");
            req.setStream(false);
            // 系统消息
            Message[] messages = new Message[2];
            Message message0 = new Message();
            message0.setRole("system");
            message0.setContent(PromptConstant.SystemPrompt);
            messages[0] = message0;
            // 用户消息
            Message message1 = new Message();
            message1.setRole("user");
            message1.setContent(message);
            messages[1] = message1;
            req.setMessages(messages);
            // 返回的resp是一个ChatCompletionsResponse的实例，与请求对象对应
            ChatCompletionsResponse resp = deepSeekClient.ChatCompletions(req);
            String content = resp.getChoices()[0].getMessage().getContent();
            // 输出json格式的字符串回包
            return content;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            log.error("AI 对话失败", e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "AI 对话失败");
        }
    }
}
